<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 基本数据类型</h1>
        <p>
            Python 中的变量不需要声明。每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。</p>
        <p>在 Python 中，变量就是变量，它没有类型，我们所说的"类型"是变量所指的内存中对象的类型。</p>
        <p>等号（=）用来给变量赋值。</p>
        <p>
            等号（=）运算符左边是一个变量名,等号（=）运算符右边是存储在变量中的值。例如：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">counter</span><span class="hl-code"> = </span><span class="hl-number">100</span><span
                        class="hl-code">          </span><span class="hl-comment"># 整型变量</span><span class="hl-code">
</span><span class="hl-identifier">miles</span><span class="hl-code">   = </span><span
                        class="hl-number">1000.0</span><span class="hl-code">       </span><span class="hl-comment"># 浮点型变量</span><span
                        class="hl-code">
</span><span class="hl-identifier">name</span><span class="hl-code">    = </span><span class="hl-quotes">"</span><span
                        class="hl-string">runoob</span><span class="hl-quotes">"</span><span
                        class="hl-code">     </span><span class="hl-comment"># 字符串</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">counter</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">miles</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">name</span><span class="hl-brackets">)</span></pre>
            </div>
            <br>
        </div>
        <p>执行以上程序会输出如下结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">100</span><span class="pln">
</span><span class="lit">1000.0</span><span class="pln">
runoob</span></pre>
        <h3>多个变量赋值</h3>
        <p>
            Python允许你同时为多个变量赋值。例如：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">a </span><span class="pun">=</span><span
                class="pln"> b </span><span class="pun">=</span><span class="pln"> c </span><span
                class="pun">=</span><span class="pln"> </span><span class="lit">1</span></pre>
        <p>
            以上实例，创建一个整型对象，值为1，三个变量被分配到相同的内存空间上。
        </p>
        <p>
            您也可以为多个对象指定多个变量。例如：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">a</span><span class="pun">,</span><span
                class="pln"> b</span><span class="pun">,</span><span class="pln"> c </span><span
                class="pun">=</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">"runoob"</span></pre>
        <p>
            以上实例，两个整型对象 1 和 2 的分配给变量 a 和 b，字符串对象 "runoob" 分配给变量 c。</p>

        <h2>标准数据类型</h2>
        <p>Python3 中有六个标准的数据类型：
        </p>
        <ul>
            <li>Number（数字）</li>
            <li>String（字符串）</li>
            <li>List（列表）</li>
            <li>Tuple（元组）</li>
            <li>Sets（集合）</li>
            <li>Dictionary（字典）</li>
        </ul>

        <h2>Number（数字）</h2>
        <p>Python3 支持 <b>int、float、bool、complex（复数）</b>。
        </p>
        <p>在Python 3里，只有一种整数类型 int，表示为长整型，没有 python2 中的 Long。</p>
        <p>像大多数语言一样，数值类型的赋值和计算都是很直观的。</p>
        <p>内置的 type() 函数可以用来查询变量所指的对象类型。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a</span><span
                class="pun">,</span><span class="pln"> b</span><span class="pun">,</span><span
                class="pln"> c</span><span class="pun">,</span><span class="pln"> d </span><span
                class="pun">=</span><span class="pln"> </span><span class="lit">20</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">5.5</span><span class="pun">,</span><span
                class="pln"> </span><span class="kwd">True</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">4</span><span class="pun">+</span><span class="lit">3j</span><span
                class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">type</span><span class="pun">(</span><span
                class="pln">a</span><span class="pun">),</span><span class="pln"> type</span><span
                class="pun">(</span><span class="pln">b</span><span class="pun">),</span><span
                class="pln"> type</span><span class="pun">(</span><span class="pln">c</span><span
                class="pun">),</span><span class="pln"> type</span><span class="pun">(</span><span
                class="pln">d</span><span class="pun">))</span><span class="pln">
</span><span class="pun">&lt;</span><span class="kwd">class</span><span class="pln"> </span><span
                class="str">'int'</span><span class="pun">&gt;</span><span class="pln"> </span><span
                class="pun">&lt;</span><span class="kwd">class</span><span class="pln"> </span><span
                class="str">'float'</span><span class="pun">&gt;</span><span class="pln"> </span><span
                class="pun">&lt;</span><span class="kwd">class</span><span class="pln"> </span><span
                class="str">'bool'</span><span class="pun">&gt;</span><span class="pln"> </span><span
                class="pun">&lt;</span><span class="kwd">class</span><span class="pln"> </span><span class="str">'complex'</span><span
                class="pun">&gt;</span></pre>

        <p>此外还可以用 isinstance 来判断：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-code">&gt;&gt;&gt;</span><span class="hl-identifier">a</span><span
                        class="hl-code"> = </span><span class="hl-number">111</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-builtin">isinstance</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">a</span><span class="hl-code">, </span><span
                        class="hl-identifier">int</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">True</span><span class="hl-code">
&gt;&gt;&gt;</span></pre>
            </div>
        </div>
        <p>
            isinstance 和 type 的区别在于：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">class</span><span class="pln"> A</span><span
                class="pun">:</span><span class="pln">
    </span><span class="kwd">pass</span><span class="pln">

</span><span class="kwd">class</span><span class="pln"> B</span><span class="pun">(</span><span
                class="pln">A</span><span class="pun">):</span><span class="pln">
    </span><span class="kwd">pass</span><span class="pln">

isinstance</span><span class="pun">(</span><span class="pln">A</span><span class="pun">(),</span><span
                class="pln"> A</span><span class="pun">)</span><span class="pln">  </span><span class="com"># returns True</span><span
                class="pln">
type</span><span class="pun">(</span><span class="pln">A</span><span class="pun">())</span><span
                class="pln"> </span><span class="pun">==</span><span class="pln"> A      </span><span class="com"># returns True</span><span
                class="pln">
isinstance</span><span class="pun">(</span><span class="pln">B</span><span class="pun">(),</span><span
                class="pln"> A</span><span class="pun">)</span><span class="pln">    </span><span class="com"># returns True</span><span
                class="pln">
type</span><span class="pun">(</span><span class="pln">B</span><span class="pun">())</span><span
                class="pln"> </span><span class="pun">==</span><span class="pln"> A        </span><span class="com"># returns False</span></pre>
        <p>
            区别就是: </p>
        <ul>
            <li>
                type()不会认为子类是一种父类类型。
            </li>
            <li>
                isinstance()会认为子类是一种父类类型。
            </li>
        </ul>

        <blockquote>
            <p><b>注意：</b>在 Python2 中是没有布尔型的，它用数字 0 表示 False，用 1 表示 True。到 Python3 中，把 True 和 False 定义成关键字了，但它们的值还是 1 和
                0，它们可以和数字相加。</p>
        </blockquote>
        <p>当你指定一个值时，Number 对象就会被创建：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">var1 </span><span class="pun">=</span><span
                class="pln"> </span><span class="lit">1</span><span class="pln">
var2 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">10</span></pre>
        <p>
            您也可以使用del语句删除一些对象引用。</p>
        <p>
            del语句的语法是：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">del</span><span class="pln"> var1</span><span
                class="pun">[,</span><span class="pln">var2</span><span class="pun">[,</span><span
                class="pln">var3</span><span class="pun">[....,</span><span class="pln">varN</span><span class="pun">]]]]</span></pre>
        <p>您可以通过使用del语句删除单个或多个对象。例如：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">del</span><span class="pln"> </span><span
                class="kwd">var</span><span class="pln">
</span><span class="kwd">del</span><span class="pln"> var_a</span><span class="pun">,</span><span
                class="pln"> var_b</span></pre>
        <h3>数值运算</h3>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-code">&gt;&gt;&gt;</span><span class="hl-number">5</span><span
                        class="hl-code"> + </span><span class="hl-number">4</span><span class="hl-code">  </span><span
                        class="hl-comment"># 加法</span><span class="hl-code">
</span><span class="hl-number">9</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-number">4.3</span><span class="hl-code"> - </span><span
                        class="hl-number">2</span><span class="hl-code"> </span><span
                        class="hl-comment"># 减法</span><span class="hl-code">
</span><span class="hl-number">2.3</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-number">3</span><span class="hl-code"> * </span><span
                        class="hl-number">7</span><span class="hl-code">  </span><span
                        class="hl-comment"># 乘法</span><span class="hl-code">
</span><span class="hl-number">21</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-number">2</span><span class="hl-code"> / </span><span
                        class="hl-number">4</span><span class="hl-code">  </span><span
                        class="hl-comment"># 除法，得到一个浮点数</span><span class="hl-code">
</span><span class="hl-number">0.5</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-number">2</span><span class="hl-code"> // </span><span
                        class="hl-number">4</span><span class="hl-code"> </span><span
                        class="hl-comment"># 除法，得到一个整数</span><span class="hl-code">
</span><span class="hl-number">0</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-number">17</span><span class="hl-code"> % </span><span
                        class="hl-number">3</span><span class="hl-code"> </span><span
                        class="hl-comment"># 取余 </span><span class="hl-code">
</span><span class="hl-number">2</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-number">2</span><span class="hl-code"> ** </span><span
                        class="hl-number">5</span><span class="hl-code"> </span><span
                        class="hl-comment"># 乘方</span><span class="hl-code">
</span><span class="hl-number">32</span></pre>
            </div>
        </div>
        <p><b>注意：</b></p>
        <ul>
            <li>1、Python可以同时为多个变量赋值，如a, b = 1, 2。</li>
            <li>
                2、一个变量可以通过赋值指向不同类型的对象。
            </li>
            <li>
                3、数值的除法（/）总是返回一个浮点数，要获取整数使用//操作符。
            </li>
            <li>
                4、在混合计算时，Python会把整型转换成为浮点数。
            </li>
        </ul>
        <h3>数值类型实例</h3>
        <table class="reference">
            <tbody>
            <tr>
                <th>int</th>
                <th>float</th>
                <th>complex</th>
            </tr>
            <tr>
                <td>10</td>
                <td>0.0</td>
                <td>3.14j</td>
            </tr>
            <tr>
                <td>100</td>
                <td>15.20</td>
                <td>45.j</td>
            </tr>
            <tr>
                <td>-786</td>
                <td>-21.9</td>
                <td>9.322e-36j</td>
            </tr>
            <tr>
                <td>080</td>
                <td>32.3e+18</td>
                <td>.876j</td>
            </tr>
            <tr>
                <td>-0490</td>
                <td>-90.</td>
                <td>-.6545+0J</td>
            </tr>
            <tr>
                <td>-0x260</td>
                <td>-32.54e100</td>
                <td>3e+26J</td>
            </tr>
            <tr>
                <td>0x69</td>
                <td>70.2E-12</td>
                <td>4.53e-7j</td>
            </tr>
            </tbody>
        </table>
        <p>Python还支持复数，复数由实数部分和虚数部分构成，可以用a + bj,或者complex(a,b)表示， 复数的实部a和虚部b都是浮点型</p>

        <h2>String（字符串）</h2>
        <p>Python中的字符串用单引号(')或双引号(")括起来，同时使用反斜杠(\)转义特殊字符。</p>
        <p>字符串的截取的语法格式如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">变量[头下标:尾下标]</span></pre>
        <p>索引值以 0 为开始值，-1 为从末尾的开始位置。</p>
        <p>加号 (+) 是字符串的连接符， 星号 (*) 表示复制当前字符串，紧跟的数字为复制的次数。实例如下：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">

                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">str</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span
                        class="hl-string">Runoob</span><span class="hl-quotes">'</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">str</span><span class="hl-brackets">)</span><span
                        class="hl-code">          </span><span class="hl-comment"># 输出字符串</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">str</span><span class="hl-brackets">[</span><span
                        class="hl-number">0</span><span class="hl-code">:-</span><span class="hl-number">1</span><span
                        class="hl-brackets">]</span><span class="hl-brackets">)</span><span
                        class="hl-code">    </span><span class="hl-comment"># 输出第一个到倒数第二个的所有字符</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">str</span><span class="hl-brackets">[</span><span
                        class="hl-number">0</span><span class="hl-brackets">]</span><span
                        class="hl-brackets">)</span><span class="hl-code">       </span><span class="hl-comment"># 输出字符串第一个字符</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">str</span><span class="hl-brackets">[</span><span
                        class="hl-number">2</span><span class="hl-code">:</span><span class="hl-number">5</span><span
                        class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">     </span><span
                        class="hl-comment"># 输出从第三个开始到第五个的字符</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">str</span><span class="hl-brackets">[</span><span
                        class="hl-number">2</span><span class="hl-code">:</span><span class="hl-brackets">]</span><span
                        class="hl-brackets">)</span><span class="hl-code">      </span><span class="hl-comment"># 输出从第三个开始的后的所有字符</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">str</span><span class="hl-code"> * </span><span class="hl-number">2</span><span
                        class="hl-brackets">)</span><span class="hl-code">      </span><span class="hl-comment"># 输出字符串两次</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">str</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span
                        class="hl-string">TEST</span><span class="hl-quotes">"</span><span
                        class="hl-brackets">)</span><span class="hl-code"> </span><span
                        class="hl-comment"># 连接字符串</span>
                </pre>
            </div>
        </div>
        <p>执行以上程序会输出如下结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Runoob</span><span class="pln">
</span><span class="typ">Runoo</span><span class="pln">
R
noo
noob
</span><span class="typ">RunoobRunoob</span><span class="pln">
</span><span class="typ">RunoobTEST</span></pre>
        <p>Python 使用反斜杠(\)转义特殊字符，如果你不想让反斜杠发生转义，可以在字符串前面添加一个 r，表示原始字符串：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">print</span><span class="pun">(</span><span
                class="str">'Ru\noob'</span><span class="pun">)</span><span class="pln">
</span><span class="typ">Ru</span><span class="pln">
oob
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">r</span><span class="str">'Ru\noob'</span><span
                class="pun">)</span><span class="pln">
</span><span class="typ">Ru</span><span class="pln">\noob
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span></pre>
        <p>
            另外，反斜杠(\)可以作为续行符，表示下一行是上一行的延续。也可以使用 <b>"""..."""</b> 或者 <b>'''...'''</b> 跨越多行。
        </p>


        <p>注意，Python 没有单独的字符类型，一个字符就是长度为1的字符串。
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-code">&gt;&gt;&gt;</span><span
                        class="hl-identifier">word</span><span class="hl-code"> = </span><span
                        class="hl-quotes">'</span><span class="hl-string">Python</span><span
                        class="hl-quotes">'</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">word</span><span class="hl-brackets">[</span><span
                        class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-code">, </span><span
                        class="hl-identifier">word</span><span class="hl-brackets">[</span><span
                        class="hl-number">5</span><span class="hl-brackets">]</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">P</span><span class="hl-code"> </span><span class="hl-identifier">n</span><span
                        class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">word</span><span class="hl-brackets">[</span><span
                        class="hl-code">-</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span
                        class="hl-code">, </span><span class="hl-identifier">word</span><span
                        class="hl-brackets">[</span><span class="hl-code">-</span><span class="hl-number">6</span><span
                        class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">n</span><span class="hl-code"> </span><span class="hl-identifier">P</span></pre>
            </div>
        </div>


        <p>与 C 字符串不同的是，Python 字符串不能被改变。向一个索引位置赋值，比如word[0] = 'm'会导致错误。
        </p>
        <p>
            <b>注意：</b></p>
        <ul>
            <li>
                1、反斜杠可以用来转义，使用r可以让反斜杠不发生转义。
            </li>
            <li>
                2、字符串可以用+运算符连接在一起，用*运算符重复。
            </li>
            <li>
                3、Python中的字符串有两种索引方式，从左往右以0开始，从右往左以-1开始。
            </li>
            <li>
                4、Python中的字符串不能改变。
            </li>
        </ul>

        <h2>List（列表）</h2>
        <p>List（列表） 是 Python 中使用最频繁的数据类型。</p>
        <p>列表可以完成大多数集合类的数据结构实现。列表中元素的类型可以不相同，它支持数字，字符串甚至可以包含列表（所谓嵌套）。</p>
        <p>列表是写在方括号([])之间、用逗号分隔开的元素列表。</p>
        <p>
            和字符串一样，列表同样可以被索引和截取，列表被截取后返回一个包含所需元素的新列表。
        </p>
        <p>列表截取的语法格式如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">变量[头下标:尾下标]</span></pre>
        <p>索引值以 0 为开始值，-1 为从末尾的开始位置。</p>
        <p>加号（+）是列表连接运算符，星号（*）是重复操作。如下实例：
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">list</span><span class="hl-code"> = </span><span class="hl-brackets">[</span><span
                        class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">abcd</span><span
                        class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-number">786</span><span
                        class="hl-code"> , </span><span class="hl-number">2.23</span><span
                        class="hl-code">, </span><span class="hl-quotes">'</span><span
                        class="hl-string">runoob</span><span class="hl-quotes">'</span><span
                        class="hl-code">, </span><span class="hl-number">70.2</span><span class="hl-code"> </span><span
                        class="hl-brackets">]</span><span class="hl-code">
</span><span class="hl-identifier">tinylist</span><span class="hl-code"> = </span><span
                        class="hl-brackets">[</span><span class="hl-number">123</span><span
                        class="hl-code">, </span><span class="hl-quotes">'</span><span
                        class="hl-string">runoob</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">]</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">list</span><span class="hl-brackets">)</span><span
                        class="hl-code">            </span><span class="hl-comment"># 输出完整列表</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">list</span><span class="hl-brackets">[</span><span
                        class="hl-number">0</span><span class="hl-brackets">]</span><span
                        class="hl-brackets">)</span><span class="hl-code">         </span><span class="hl-comment"># 输出列表第一个元素</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">list</span><span class="hl-brackets">[</span><span
                        class="hl-number">1</span><span class="hl-code">:</span><span class="hl-number">3</span><span
                        class="hl-brackets">]</span><span class="hl-brackets">)</span><span
                        class="hl-code">       </span><span class="hl-comment"># 从第二个开始输出到第三个元素</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">list</span><span class="hl-brackets">[</span><span
                        class="hl-number">2</span><span class="hl-code">:</span><span class="hl-brackets">]</span><span
                        class="hl-brackets">)</span><span class="hl-code">        </span><span class="hl-comment"># 输出从第三个元素开始的所有元素</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">tinylist</span><span class="hl-code"> * </span><span
                        class="hl-number">2</span><span class="hl-brackets">)</span><span
                        class="hl-code">    </span><span class="hl-comment"># 输出两次列表</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">list</span><span class="hl-code"> + </span><span class="hl-identifier">tinylist</span><span
                        class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-comment"># 连接列表</span>
                </pre>
            </div>
        </div>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">[</span><span class="str">'abcd'</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">786</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">2.23</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'runoob'</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">70.2</span><span
                class="pun">]</span><span class="pln">
abcd
</span><span class="pun">[</span><span class="lit">786</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">2.23</span><span class="pun">]</span><span class="pln">
</span><span class="pun">[</span><span class="lit">2.23</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'runoob'</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">70.2</span><span class="pun">]</span><span class="pln">
</span><span class="pun">[</span><span class="lit">123</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'runoob'</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">123</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'runoob'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">[</span><span class="str">'abcd'</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">786</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">2.23</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'runoob'</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">70.2</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">123</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'runoob'</span><span class="pun">]</span></pre>

        <p>与Python字符串不一样的是，列表中的元素是可以改变的：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-code">&gt;&gt;&gt;</span><span class="hl-identifier">a</span><span
                        class="hl-code"> = </span><span class="hl-brackets">[</span><span
                        class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">2</span><span
                        class="hl-code">, </span><span class="hl-number">3</span><span class="hl-code">, </span><span
                        class="hl-number">4</span><span class="hl-code">, </span><span class="hl-number">5</span><span
                        class="hl-code">, </span><span class="hl-number">6</span><span class="hl-brackets">]</span><span
                        class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">a</span><span class="hl-brackets">[</span><span
                        class="hl-number">0</span><span class="hl-brackets">]</span><span
                        class="hl-code"> = </span><span class="hl-number">9</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">a</span><span class="hl-brackets">[</span><span
                        class="hl-number">2</span><span class="hl-code">:</span><span class="hl-number">5</span><span
                        class="hl-brackets">]</span><span class="hl-code"> = </span><span
                        class="hl-brackets">[</span><span class="hl-number">13</span><span
                        class="hl-code">, </span><span class="hl-number">14</span><span class="hl-code">, </span><span
                        class="hl-number">15</span><span class="hl-brackets">]</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">a</span><span class="hl-code">
</span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span
                        class="hl-number">2</span><span class="hl-code">, </span><span class="hl-number">13</span><span
                        class="hl-code">, </span><span class="hl-number">14</span><span class="hl-code">, </span><span
                        class="hl-number">15</span><span class="hl-code">, </span><span class="hl-number">6</span><span
                        class="hl-brackets">]</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">a</span><span class="hl-brackets">[</span><span
                        class="hl-number">2</span><span class="hl-code">:</span><span class="hl-number">5</span><span
                        class="hl-brackets">]</span><span class="hl-code"> = </span><span
                        class="hl-brackets">[</span><span class="hl-brackets">]</span><span
                        class="hl-code">   </span><span class="hl-comment"># 将对应的元素值设置为 [] </span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">a</span><span class="hl-code">
</span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span
                        class="hl-number">2</span><span class="hl-code">, </span><span class="hl-number">6</span><span
                        class="hl-brackets">]</span></pre>
            </div>
        </div>
        <p>List内置了有很多方法，例如append()、pop()等等，这在后面会讲到。</p>
        <p><b>注意：</b></p>
        <ul>
            <li>
                1、List写在方括号之间，元素用逗号隔开。
            </li>
            <li>
                2、和字符串一样，list可以被索引和切片。
            </li>
            <li>
                3、List可以使用+操作符进行拼接。
            </li>
            <li>
                4、List中的元素是可以改变的。
            </li>
        </ul>


        <h2>Tuple（元组）</h2>
        <p>元组（tuple）与列表类似，不同之处在于元组的元素不能修改。元组写在小括号(<b>()</b>)里，元素之间用逗号隔开。
        </p>
        <p>元组中的元素类型也可以不相同：
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">tuple</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">abcd</span><span
                        class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-number">786</span><span
                        class="hl-code"> , </span><span class="hl-number">2.23</span><span
                        class="hl-code">, </span><span class="hl-quotes">'</span><span
                        class="hl-string">runoob</span><span class="hl-quotes">'</span><span
                        class="hl-code">, </span><span class="hl-number">70.2</span><span class="hl-code">  </span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">tinytuple</span><span class="hl-code"> = </span><span
                        class="hl-brackets">(</span><span class="hl-number">123</span><span
                        class="hl-code">, </span><span class="hl-quotes">'</span><span
                        class="hl-string">runoob</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">tuple</span><span class="hl-brackets">)</span><span
                        class="hl-code">             </span><span class="hl-comment"># 输出完整元组</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">tuple</span><span class="hl-brackets">[</span><span
                        class="hl-number">0</span><span class="hl-brackets">]</span><span
                        class="hl-brackets">)</span><span class="hl-code">          </span><span class="hl-comment"># 输出元组的第一个元素</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">tuple</span><span class="hl-brackets">[</span><span
                        class="hl-number">1</span><span class="hl-code">:</span><span class="hl-number">3</span><span
                        class="hl-brackets">]</span><span class="hl-brackets">)</span><span
                        class="hl-code">        </span><span class="hl-comment"># 输出从第二个元素开始到第三个元素</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">tuple</span><span class="hl-brackets">[</span><span
                        class="hl-number">2</span><span class="hl-code">:</span><span class="hl-brackets">]</span><span
                        class="hl-brackets">)</span><span class="hl-code">         </span><span class="hl-comment"># 输出从第三个元素开始的所有元素</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">tinytuple</span><span class="hl-code"> * </span><span
                        class="hl-number">2</span><span class="hl-brackets">)</span><span
                        class="hl-code">     </span><span class="hl-comment"># 输出两次元组</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">tuple</span><span class="hl-code"> + </span><span class="hl-identifier">tinytuple</span><span
                        class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-comment"># 连接元组</span>
                </pre>
            </div>
        </div>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">(</span><span class="str">'abcd'</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">786</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">2.23</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'runoob'</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">70.2</span><span
                class="pun">)</span><span class="pln">
abcd
</span><span class="pun">(</span><span class="lit">786</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">2.23</span><span class="pun">)</span><span class="pln">
</span><span class="pun">(</span><span class="lit">2.23</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'runoob'</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">70.2</span><span class="pun">)</span><span class="pln">
</span><span class="pun">(</span><span class="lit">123</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'runoob'</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">123</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'runoob'</span><span class="pun">)</span><span class="pln">
</span><span class="pun">(</span><span class="str">'abcd'</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">786</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">2.23</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'runoob'</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">70.2</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">123</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'runoob'</span><span class="pun">)</span></pre>
        <p>元组与字符串类似，可以被索引且下标索引从0开始，-1 为从末尾开始的位置。也可以进行截取（看上面，这里不再赘述）。</p>
        <p>其实，可以把字符串看作一种特殊的元组。
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-code">&gt;&gt;&gt;</span><span
                        class="hl-identifier">tup</span><span class="hl-code"> = </span><span
                        class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-code">, </span><span
                        class="hl-number">2</span><span class="hl-code">, </span><span class="hl-number">3</span><span
                        class="hl-code">, </span><span class="hl-number">4</span><span class="hl-code">, </span><span
                        class="hl-number">5</span><span class="hl-code">, </span><span class="hl-number">6</span><span
                        class="hl-brackets">)</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">tup</span><span class="hl-brackets">[</span><span
                        class="hl-number">0</span><span class="hl-brackets">]</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-number">1</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">tup</span><span class="hl-brackets">[</span><span
                        class="hl-number">1</span><span class="hl-code">:</span><span class="hl-number">5</span><span
                        class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">, </span><span
                        class="hl-number">3</span><span class="hl-code">, </span><span class="hl-number">4</span><span
                        class="hl-code">, </span><span class="hl-number">5</span><span class="hl-brackets">)</span><span
                        class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">tup</span><span class="hl-brackets">[</span><span
                        class="hl-number">0</span><span class="hl-brackets">]</span><span
                        class="hl-code"> = </span><span class="hl-number">11</span><span class="hl-code">  </span><span
                        class="hl-comment"># 修改元组元素的操作是非法的</span><span class="hl-code">
</span><span class="hl-identifier">Traceback</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">most</span><span class="hl-code"> </span><span class="hl-identifier">recent</span><span
                        class="hl-code"> </span><span class="hl-identifier">call</span><span
                        class="hl-code"> </span><span class="hl-identifier">last</span><span
                        class="hl-brackets">)</span><span class="hl-code">:
  </span><span class="hl-identifier">File</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span
                        class="hl-string">&lt;stdin&gt;</span><span class="hl-quotes">"</span><span
                        class="hl-code">, </span><span class="hl-identifier">line</span><span
                        class="hl-code"> </span><span class="hl-number">1</span><span class="hl-code">, </span><span
                        class="hl-reserved">in</span><span class="hl-code"> &lt;</span><span class="hl-identifier">module</span><span
                        class="hl-code">&gt;
</span><span class="hl-reserved">TypeError</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span
                        class="hl-string">tuple</span><span class="hl-quotes">'</span><span
                        class="hl-code"> </span><span class="hl-identifier">object</span><span
                        class="hl-code"> </span><span class="hl-identifier">does</span><span
                        class="hl-code"> </span><span class="hl-reserved">not</span><span class="hl-code"> </span><span
                        class="hl-identifier">support</span><span class="hl-code"> </span><span class="hl-identifier">item</span><span
                        class="hl-code"> </span><span class="hl-identifier">assignment</span><span class="hl-code">
&gt;&gt;&gt;</span></pre>
            </div>
        </div>
        <p>虽然tuple的元素不可改变，但它可以包含可变的对象，比如list列表。
        </p>
        <p>
            构造包含 0 个或 1 个元素的元组比较特殊，所以有一些额外的语法规则：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">tup1 </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">()</span><span class="pln">    </span><span
                class="com"># 空元组</span><span class="pln">
tup2 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span
                class="lit">20</span><span class="pun">,)</span><span class="pln"> </span><span class="com"># 一个元素，需要在元素后添加逗号</span></pre>

        <p>string、list和tuple都属于sequence（序列）。</p>
        <p><b>注意：</b></p>
        <ul>
            <li>
                1、与字符串一样，元组的元素不能修改。
            </li>
            <li>
                2、元组也可以被索引和切片，方法一样。
            </li>
            <li>
                3、注意构造包含0或1个元素的元组的特殊语法规则。
            </li>
            <li>
                4、元组也可以使用+操作符进行拼接。
            </li>
        </ul>

        <h2>Set（集合）</h2>
        <p>
            集合（set）是一个无序不重复元素的序列。</p>
        <p>基本功能是进行成员关系测试和删除重复元素。</p>
        <p>可以使用大括号 <span class="marked">{ }</span> 或者 <span class="marked">set()</span> 函数创建集合，注意：创建一个空集合必须用<span
                class="marked"> set()</span> 而不是 <span class="marked">{ }</span>，因为 <span class="marked">{ }</span>
            是用来创建一个空字典。
        </p>
        <p>创建格式：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">parame </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">{</span><span class="pln">value01</span><span class="pun">,</span><span
                class="pln">value02</span><span class="pun">,...}</span><span class="pln">
</span><span class="pun">或者</span><span class="pln">
</span><span class="kwd">set</span><span class="pun">(</span><span class="pln">value</span><span
                class="pun">)</span></pre>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">student</span><span class="hl-code"> = {</span><span class="hl-quotes">'</span><span
                        class="hl-string">Tom</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span
                        class="hl-quotes">'</span><span class="hl-string">Jim</span><span
                        class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span
                        class="hl-string">Mary</span><span class="hl-quotes">'</span><span
                        class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">Tom</span><span
                        class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span
                        class="hl-string">Jack</span><span class="hl-quotes">'</span><span
                        class="hl-code">, </span><span class="hl-quotes">'</span><span
                        class="hl-string">Rose</span><span class="hl-quotes">'</span><span class="hl-code">}

</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">student</span><span class="hl-brackets">)</span><span
                        class="hl-code">   </span><span class="hl-comment"># 输出集合，重复的元素被自动去掉</span><span
                        class="hl-code">

</span><span class="hl-comment"># 成员测试</span><span class="hl-code">
</span><span class="hl-identifier">if</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">Rose</span><span class="hl-quotes">'</span><span class="hl-code"> </span><span
                        class="hl-reserved">in</span><span class="hl-code"> </span><span
                        class="hl-identifier">student</span><span class="hl-brackets">)</span><span class="hl-code"> :
    </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">'</span><span class="hl-string">Rose 在集合中</span><span
                        class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code"> :
    </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">'</span><span class="hl-string">Rose 不在集合中</span><span
                        class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">


</span><span class="hl-comment"># set可以进行集合运算</span><span class="hl-code">
</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-identifier">set</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">abracadabra</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-identifier">set</span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">alacazam</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">a</span><span class="hl-code"> - </span><span
                        class="hl-identifier">b</span><span class="hl-brackets">)</span><span
                        class="hl-code">     </span><span class="hl-comment"># a和b的差集</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">a</span><span class="hl-code"> | </span><span
                        class="hl-identifier">b</span><span class="hl-brackets">)</span><span
                        class="hl-code">     </span><span class="hl-comment"># a和b的并集</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">a</span><span class="hl-code"> &amp; </span><span
                        class="hl-identifier">b</span><span class="hl-brackets">)</span><span
                        class="hl-code">     </span><span class="hl-comment"># a和b的交集</span><span class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">a</span><span class="hl-code"> ^ </span><span
                        class="hl-identifier">b</span><span class="hl-brackets">)</span><span
                        class="hl-code">     </span><span class="hl-comment"># a和b中不同时存在的元素</span></pre>
            </div>
        </div>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">{</span><span class="str">'Mary'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'Jim'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'Rose'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'Jack'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'Tom'</span><span
                class="pun">}</span><span class="pln">
</span><span class="typ">Rose</span><span class="pln"> </span><span class="pun">在集合中</span><span class="pln">
</span><span class="pun">{</span><span class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'c'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'r'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'d'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">{</span><span class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'d'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'r'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">{</span><span class="str">'l'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'r'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'c'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'z'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'m'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'d'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">{</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'c'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">{</span><span class="str">'l'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'r'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'z'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'m'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'d'</span><span class="pun">}</span></pre>

        <h2>Dictionary（字典）</h2>
        <p>
            字典（dictionary）是Python中另一个非常有用的内置数据类型。</p>
        <p>列表是有序的对象结合，字典是无序的对象集合。两者之间的区别在于：字典当中的元素是通过键来存取的，而不是通过偏移存取。</p>
        <p>字典是一种映射类型，字典用"{ }"标识，它是一个无序的<b>键(key) : 值(value)</b>对集合。</p>
        <p>键(key)必须使用不可变类型。</p>
        <p>在同一个字典中，键(key)必须是唯一的。
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">dict</span><span class="hl-code"> = {}
</span><span class="hl-identifier">dict</span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span
                        class="hl-string">one</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">]</span><span class="hl-code"> = </span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - 菜鸟教程</span><span
                        class="hl-quotes">"</span><span class="hl-code">
</span><span class="hl-identifier">dict</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span
                        class="hl-brackets">]</span><span class="hl-code">     = </span><span class="hl-quotes">"</span><span
                        class="hl-string">2 - 菜鸟工具</span><span class="hl-quotes">"</span><span class="hl-code">

</span><span class="hl-identifier">tinydict</span><span class="hl-code"> = {</span><span class="hl-quotes">'</span><span
                        class="hl-string">name</span><span class="hl-quotes">'</span><span
                        class="hl-code">: </span><span class="hl-quotes">'</span><span
                        class="hl-string">runoob</span><span class="hl-quotes">'</span><span
                        class="hl-code">,</span><span class="hl-quotes">'</span><span class="hl-string">code</span><span
                        class="hl-quotes">'</span><span class="hl-code">:</span><span class="hl-number">1</span><span
                        class="hl-code">, </span><span class="hl-quotes">'</span><span
                        class="hl-string">site</span><span class="hl-quotes">'</span><span
                        class="hl-code">: </span><span class="hl-quotes">'</span><span
                        class="hl-string">www.runoob.com</span><span class="hl-quotes">'</span><span class="hl-code">}


</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">dict</span><span class="hl-brackets">[</span><span
                        class="hl-quotes">'</span><span class="hl-string">one</span><span
                        class="hl-quotes">'</span><span class="hl-brackets">]</span><span
                        class="hl-brackets">)</span><span class="hl-code">       </span><span class="hl-comment"># 输出键为 'one' 的值</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">dict</span><span class="hl-brackets">[</span><span
                        class="hl-number">2</span><span class="hl-brackets">]</span><span
                        class="hl-brackets">)</span><span class="hl-code">           </span><span class="hl-comment"># 输出键为 2 的值</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">tinydict</span><span class="hl-brackets">)</span><span
                        class="hl-code">          </span><span class="hl-comment"># 输出完整的字典</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">tinydict</span><span class="hl-code">.</span><span class="hl-identifier">keys</span><span
                        class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code">   </span><span class="hl-comment"># 输出所有键</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">tinydict</span><span class="hl-code">.</span><span class="hl-identifier">values</span><span
                        class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span
                        class="hl-code"> </span><span class="hl-comment"># 输出所有值</span></pre>
            </div>
        </div>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">1</span><span class="pln"> </span><span
                class="pun">-</span><span class="pln"> </span><span class="pun">菜鸟教程</span><span class="pln">
</span><span class="lit">2</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span
                class="pun">菜鸟工具</span><span class="pln">
</span><span class="pun">{</span><span class="str">'name'</span><span class="pun">:</span><span
                class="pln"> </span><span class="str">'runoob'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'site'</span><span class="pun">:</span><span
                class="pln"> </span><span class="str">'www.runoob.com'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'code'</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">1</span><span class="pun">}</span><span class="pln">
dict_keys</span><span class="pun">([</span><span class="str">'name'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'site'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'code'</span><span class="pun">])</span><span class="pln">
dict_values</span><span class="pun">([</span><span class="str">'runoob'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'www.runoob.com'</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">1</span><span class="pun">])</span></pre>
        <p>构造函数 dict() 可以直接从键值对序列中构建字典如下：</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-code">&gt;&gt;&gt;</span><span class="hl-builtin">dict</span><span
                        class="hl-brackets">(</span><span class="hl-brackets">[</span><span class="hl-brackets">(</span><span
                        class="hl-quotes">'</span><span class="hl-string">Runoob</span><span
                        class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-number">1</span><span
                        class="hl-brackets">)</span><span class="hl-code">, </span><span
                        class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">Google</span><span class="hl-quotes">'</span><span
                        class="hl-code">, </span><span class="hl-number">2</span><span class="hl-brackets">)</span><span
                        class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">Taobao</span><span class="hl-quotes">'</span><span
                        class="hl-code">, </span><span class="hl-number">3</span><span class="hl-brackets">)</span><span
                        class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">
{</span><span class="hl-quotes">'</span><span class="hl-string">Taobao</span><span class="hl-quotes">'</span><span
                        class="hl-code">: </span><span class="hl-number">3</span><span class="hl-code">, </span><span
                        class="hl-quotes">'</span><span class="hl-string">Runoob</span><span
                        class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-number">1</span><span
                        class="hl-code">, </span><span class="hl-quotes">'</span><span
                        class="hl-string">Google</span><span class="hl-quotes">'</span><span
                        class="hl-code">: </span><span class="hl-number">2</span><span class="hl-code">}

&gt;&gt;&gt; {</span><span class="hl-identifier">x</span><span class="hl-code">: </span><span
                        class="hl-identifier">x</span><span class="hl-code">**</span><span
                        class="hl-number">2</span><span class="hl-code"> </span><span
                        class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span
                        class="hl-code"> </span><span class="hl-identifier">in</span><span class="hl-code"> </span><span
                        class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">, </span><span
                        class="hl-number">4</span><span class="hl-code">, </span><span class="hl-number">6</span><span
                        class="hl-brackets">)</span><span class="hl-code">}
{</span><span class="hl-number">2</span><span class="hl-code">: </span><span class="hl-number">4</span><span
                        class="hl-code">, </span><span class="hl-number">4</span><span class="hl-code">: </span><span
                        class="hl-number">16</span><span class="hl-code">, </span><span class="hl-number">6</span><span
                        class="hl-code">: </span><span class="hl-number">36</span><span class="hl-code">}

&gt;&gt;&gt; </span><span class="hl-builtin">dict</span><span class="hl-brackets">(</span><span class="hl-identifier">Runoob</span><span
                        class="hl-code">=</span><span class="hl-number">1</span><span class="hl-code">, </span><span
                        class="hl-identifier">Google</span><span class="hl-code">=</span><span
                        class="hl-number">2</span><span class="hl-code">, </span><span
                        class="hl-identifier">Taobao</span><span class="hl-code">=</span><span
                        class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">
{</span><span class="hl-quotes">'</span><span class="hl-string">Taobao</span><span class="hl-quotes">'</span><span
                        class="hl-code">: </span><span class="hl-number">3</span><span class="hl-code">, </span><span
                        class="hl-quotes">'</span><span class="hl-string">Runoob</span><span
                        class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-number">1</span><span
                        class="hl-code">, </span><span class="hl-quotes">'</span><span
                        class="hl-string">Google</span><span class="hl-quotes">'</span><span
                        class="hl-code">: </span><span class="hl-number">2</span><span class="hl-code">}</span></pre>
            </div>
        </div>
        <p>另外，字典类型也有一些内置的函数，例如clear()、keys()、values()等。</p>
        <p><b>注意：</b></p>
        <ul>
            <li>
                1、字典是一种映射类型，它的元素是键值对。
            </li>
            <li>
                2、字典的关键字必须为不可变类型，且不能重复。
            </li>
            <li>
                3、创建空字典使用 <b>{ }</b>。
            </li>
        </ul>

        <h2>Python数据类型转换</h2>
        <p>
            有时候，我们需要对数据内置的类型进行转换，数据类型的转换，你只需要将数据类型作为函数名即可。</p>
        <p>
            以下几个内置的函数可以执行数据类型之间的转换。这些函数返回一个新的对象，表示转换的值。</p>
        <table class="reference">
            <tbody>
            <tr>
                <th>函数</th>
                <th>描述</th>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="python-func-int.html" target="_blank">int(x [,base])</a></p>
                </td>
                <td>
                    <p>将x转换为一个整数</p>
                </td>
            </tr>

            <tr valign="top">
                <td>
                    <p><a href="python-func-float.html" target="_blank">float(x)</a></p>
                </td>
                <td>
                    <p>将x转换到一个浮点数</p>
                </td>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="python-func-complex.html" target="_blank">complex(real [,imag])</a></p>
                </td>
                <td>
                    <p>创建一个复数</p>
                </td>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="python-func-str.html" target="_blank">str(x)</a></p>
                </td>
                <td>
                    <p>将对象 x 转换为字符串</p>
                </td>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="python-func-repr.html" target="_blank">repr(x)</a></p>
                </td>
                <td>
                    <p>将对象 x 转换为表达式字符串</p>
                </td>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="python-func-eval.html" target="_blank">eval(str)</a></p>
                </td>
                <td>
                    <p>用来计算在字符串中的有效Python表达式,并返回一个对象</p>
                </td>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="/python3/python3-func-tuple.html" target="_blank">tuple(s)</a></p>
                </td>
                <td>
                    <p>将序列 s 转换为一个元组</p>

                </td>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="python3-att-list-list.html" target="_blank">list(s)</a></p>
                </td>
                <td>
                    <p>将序列 s 转换为一个列表</p>
                </td>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="python-func-set.html" target="_blank">set(s)</a></p>
                </td>
                <td>
                    <p>转换为可变集合</p>
                </td>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="python-func-dict.html" target="_blank">dict(d)</a></p>
                </td>
                <td>
                    <p>创建一个字典。d 必须是一个序列 (key,value)元组。</p>
                </td>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="python-func-frozenset.html" target="_blank">frozenset(s)</a></p>
                </td>
                <td>
                    <p>转换为不可变集合</p>
                </td>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="python-func-chr.html" target="_blank">chr(x)</a></p>
                </td>
                <td>
                    <p> 将一个整数转换为一个字符</p>
                </td>
            </tr>

            <tr valign="top">
                <td>
                    <p><a href="python-func-ord.html" target="_blank">ord(x)</a></p>
                </td>
                <td>
                    <p> 将一个字符转换为它的整数值</p>
                </td>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="python-func-hex.html" target="_blank">hex(x)</a></p>
                </td>
                <td>
                    <p> 将一个整数转换为一个十六进制字符串</p>
                </td>
            </tr>
            <tr valign="top">
                <td>
                    <p><a href="python-func-oct.html" target="_blank">oct(x)</a></p>
                </td>
                <td>
                    <p> 将一个整数转换为一个八进制字符串</p>
                </td>
            </tr>
            </tbody>
        </table>

    </div>
</div>
</body>
</html>
